import axios from 'axios'
import { useUserStore } from '@/stores/user.js'
import router from '@/router'

/*const request = axios.create({
    baseURL: '/admin',
    timeout: 10000,
    withCredentials: true
})*/

axios.defaults.baseURL = import.meta.env.VITE_BASE_URL
// axios.defaults.withCredentials = true
// axios.defaults.crossDomain = true

axios.interceptors.request.use((config) => {
  config.headers.Accept = 'application/vnd.api.v1+json'
  return config
})

axios.interceptors.response.use((res) => {
  if (res.status === 401 || res.data.code === 401) {
    const userStore = useUserStore()
    userStore.username = ''
    localStorage.setItem('username', '')
    router.push('/login')
  }
  return res.data
})

export default axios
